package cc.linker.steplink.crm.controller;

import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.response.ResponseCode;
import cc.linker.steplink.crm.service.ArticleDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/articledata")
public class ArticleDataController {

    @Autowired
    private ArticleDataService articleDataService;

    @Autowired
    private RequestFilter requestFilter;
    
    @Autowired
    private UserInfoUtil userInfoUtil;
    /**
     * 文章总数据
     *
     * @param customerId 客户id(web)/2个地方同一个接口，下同
     * @return
     * @throws GlobalException
     */
    @GetMapping("/data")
    public Object data(String customerId) throws GlobalException {
        if (!StringUtils.isEmpty(customerId)) {
            return articleDataService.getDataByCustomerId(customerId);
        } else {
            throw new GlobalException(ResponseCode.ID_NULL);
        }
    }

    @GetMapping("/h5/data")
    public Object h5data(HttpServletRequest request) throws GlobalException {
        WxUser wxUser =  requestFilter.getWxUser(request);
        if (!StringUtils.isEmpty(wxUser.getCustomerId())) {
            return articleDataService.getDataByCustomerId(wxUser.getCustomerId());
        } else {
            throw new GlobalException(ResponseCode.ID_NULL);
        }
    }


    /**
     * 文章每日数据
     *
     * @param pn         页码
     * @param customerId 客户id(web)
     * @param start      开始时间
     * @param end        结束时间
     * @return
     * @throws GlobalException
     */
    @GetMapping("/log")
    public Object log(Integer pn, String customerId, String start, String end) throws GlobalException {
        if (!StringUtils.isEmpty(customerId)) {
            return articleDataService.getLogByCustomerId(customerId, pn, start, end);
        } else {
            throw new GlobalException(ResponseCode.ID_NULL);
        }
    }

    @GetMapping("/h5/log")
    public Object h5log(HttpServletRequest request, Integer pn, String start, String end) throws GlobalException {
        WxUser wxUser =  requestFilter.getWxUser(request);
        if (!StringUtils.isEmpty(wxUser.getOpenid()) && !StringUtils.isEmpty(wxUser.getToken())) {
            return articleDataService.getLogByCustomerId(wxUser.getCustomerId() , pn);
        } else {
            throw new GlobalException(ResponseCode.ID_NULL);
        }
    }

    /**
     * 文章推广列表
     *
     * @param pn       页码
     * @param pageSize 页面大小
     * @param session  登录用户信息
     * @return
     * @throws GlobalException
     */
    @GetMapping("/list")
    public Object list(Integer pn, Integer pageSize, HttpServletRequest request) throws GlobalException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        return articleDataService.listData(pn, pageSize, userinfo.getOrgId());
    }

}
